/*
Copyright (C) 2007 Kamal Singh.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

wmp = WMPEmbed.object;
var playing = true;

function view_onOpen() {
  getRadioSchedule();
}

function volume_onchange() {
  if( wmp == null ) return;
  wmp.settings.volume = volumeController.value;
}

function stop_onclick() {
	wmp.controls.stop();
	playing = false;
	playButton.visible = true;
	stopButton.visible = false;}

function play_onclick() {
	wmp.controls.play();
	playing = true;
	playButton.visible = false;
	stopButton.visible = true;
}

function downButtonHandler() {
  view.height = 87;
  downButton.visible = false;
  upButton.visible = true;
  scheduleArea.visible = true;
  next1.visible = true;
  next2.visible = true;
}

function upButtonHandler() {
  view.height = 56;
  upButton.visible = false;
  downButton.visible = true;
  scheduleArea.visible = false;
}

function displayResults() {
  debug.trace("trying to get feed");
  try {
    var nItems = results.length;
  }
  catch (err) {
    return;
  }
  
  try {
    next1.innerText = results[0].title;
    next1.href = results[0].link;
    next1.tooltip = results[0].desc;
  }
  catch (err) {
    next1.innerText = "No info available!";
  }
  try {
    next2.innerText = results[1].title;
    next2.href = results[1].link;
    next2.tooltip = results[1].desc;	
  }
  catch (err) {
    next2.innerText = "No info available!";
  }
}

function getRadioSchedule() {
  var feedRequest = null;
  
  feedRequest = new XMLHttpRequest();
  try {
    feedRequest.open("GET", "http://www.kqed.org/rss/schedule-r.xml", true);
  } 
  catch (e) {
    // Catch invalid URLs
    feedRequest = null;
    return;
  }
  
  // Set the callback for when the downloading is completed (or failed)
  feedRequest.onreadystatechange = onFeedLoad;
  
  // Start the download
  try {
    feedRequest.send();
  }
  catch (e) {
    // Catch errors sending the request
    feedRequest = null;
    return;
  }
  
  function onFeedLoad() {
    // Verify that the download completed
    if (feedRequest.readyState != 4)
      return;

    // Verify that the download was successful
    if (feedRequest.status != 200) {
      feedRequest = null;
      return;
    }
    
    var doc = new DOMDocument();
    doc.loadXML(feedRequest.responseText);
    
    var elem = doc.getElementsByTagName("item");
    results = [];
    
    if (elem != null && elem.length > 0) {
      for (var z = 0; z < elem.length; z++) {
        var title, link, desc;

        for (var node = elem[z].firstChild; node != null; node = node.nextSibling) {
          if (node.nodeName == "title") {
            try {
              title = node.firstChild.nodeValue;
            }
            catch (e) {
              title = "";
            }  
          }
          if (node.nodeName == "link") {
            try {
              link = node.firstChild.nodeValue;
            }            	
            catch (e) {
              link = "";
            }
          }
          if (node.nodeName == "description") {
            try {
              desc = node.firstChild.nodeValue;
              desc = desc.replace(/<([^>]|\n)*>/g,'');
              desc = desc.replace(/&nbsp;/g, ' ');
              desc = desc.replace(/&quot;/g, '"');
              desc = desc.replace(/&amp;/g, '&');
            }            	
            catch (e) {
              desc = "";
            }
          }
          results[z] = {title:title, link:link, desc:desc};
        }
      }
    }
    displayResults();
    feedRequest = null;
  }
}

view.setInterval(getRadioSchedule, 1000*60*5);